/**
 * 登录状态管理
 */
import { ref, computed } from 'vue'
import { getToken, removeToken } from '../api'

// 登录状态
const isLoggedIn = ref(false)
const userInfo = ref<any>(null)

// 检查登录状态
const checkLoginStatus = () => {
  const token = getToken()
  isLoggedIn.value = !!token
  return isLoggedIn.value
}

// 设置登录状态
const setLoginStatus = (loggedIn: boolean, user?: any) => {
  isLoggedIn.value = loggedIn
  userInfo.value = user || null
}

// 登出
const logout = () => {
  removeToken()
  isLoggedIn.value = false
  userInfo.value = null
}

// 计算属性
const loginStatus = computed(() => isLoggedIn.value)
const currentUser = computed(() => userInfo.value)

export {
  isLoggedIn,
  userInfo,
  checkLoginStatus,
  setLoginStatus,
  logout,
  loginStatus,
  currentUser
}
